/**
 *=============================================================================
 *  @copyright (C)      2025CCUT_1024lab_Damocles
 *
 *  @file    template.c
 *  @brief   封装模块代码规范
 *
 *  @note    使用方法
 *
 *  @attention 注意事项
 *-----------------------------------------------------------------------------
 *  @version    @author    @date    @note
 *  V1.0.0      Cherno    250808    done
 *
 *==============================================================================
 */

/*==============================Includes======================================*/
#include <string.h>

#include "dvc_DR16.h"
/*==============================PrivateMacros=================================*/

/*==============================PrivateTypes==================================*/

/*==============================PrivateVariables==============================*/

/*==============================PrivateFunctionDeclarations===================*/

/*==============================FunctionPrototypes============================*/

/**
 * @brief : 描述该函数主要功能
 * @note : 注意事项
 *
 * @param : 参数名（与函数里的参数一致）  参数的描述
 * @return : 返回值的说明
 */
void DR16::init(UART_HandleTypeDef *_huart)
{
	if (_huart->Instance == USART1) {
		uart_manager = &uart1_manager;
	} else if (_huart->Instance == USART2) {
		uart_manager = &uart2_manager;
	} else if (_huart->Instance == USART3) {
		uart_manager = &uart3_manager;
	} else if (_huart->Instance == UART4) {
		uart_manager = &uart4_manager;
	} else if (_huart->Instance == UART5) {
		uart_manager = &uart5_manager;
	} else if (_huart->Instance == USART6) {
		uart_manager = &uart6_manager;
	}
}

void DR16::judge_switch(switch_status_e *_switch, uint8_t _status,
                        uint8_t _pre_status)
{
	switch (_pre_status) {
		case (SWITCH_UP): {
			switch (_status) {
				case (SWITCH_UP): {
					*_switch = UP;
				} break;
				case (SWITCH_DOWN): {
					*_switch = TRIG_MIDDLE_DOWN;
				} break;
				case (SWITCH_MIDDLE): {
					*_switch = TRIG_UP_MIDDLE;
				} break;
			}
			break;
		} break;
		case (SWITCH_DOWN): {
			switch (_status) {
				case (SWITCH_UP): {
					*_switch = TRIG_MIDDLE_UP;
				} break;
				case (SWITCH_DOWN): {
					*_switch = DOWN;
				} break;
				case (SWITCH_MIDDLE): {
					*_switch = TRIG_DOWN_MIDDLE;
				} break;
			}
		} break;
		case (SWITCH_MIDDLE): {
			switch (_status) {
				case (SWITCH_UP): {
					*_switch = TRIG_MIDDLE_UP;
				} break;
				case (SWITCH_DOWN): {
					*_switch = TRIG_MIDDLE_DOWN;
				} break;
				case (SWITCH_MIDDLE): {
					*_switch = MIDDLE;
				} break;
			}
		} break;
	}
}

void DR16::judge_key(key_status_e *_key, uint8_t _status, uint8_t _pre_status)
{
	switch (_pre_status) {
		case (KEY_FREE): {
			switch (_status) {
				case (KEY_FREE): {
					*_key = FREE;
				} break;
				case (KEY_PRESSED): {
					*_key = TRIG_FREE_PRESSED;
				} break;
			}
		} break;
		case (KEY_PRESSED): {
			switch (_status) {
				case (KEY_FREE): {
					*_key = TRIG_PRESSED_FREE;
				} break;
				case (KEY_PRESSED): {
					*_key = PRESSED;
				} break;
			}
		} break;
	}
}

void DR16::data_pross()
{
	// 数据处理过程
	dr16_uart_data_t *tmp_buffer = (dr16_uart_data_t *)uart_manager->rx_buffer;

	// 摇杆信息
	data.right_X = (tmp_buffer->channel_0 - rocker_offset) / rocker_num;
	data.right_Y = (tmp_buffer->channel_1 - rocker_offset) / rocker_num;
	data.left_X = (tmp_buffer->channel_2 - rocker_offset) / rocker_num;
	data.left_Y = (tmp_buffer->channel_3 - rocker_offset) / rocker_num;

	// 判断拨码触发
	judge_switch(&data.left_switch, tmp_buffer->switch_1,
	             pre_uart_rx_data.switch_1);
	judge_switch(&data.right_switch, tmp_buffer->switch_2,
	             pre_uart_rx_data.switch_2);

	// 鼠标信息
	data.mouse_X = tmp_buffer->mouse_X / 32768.0f;
	data.mouse_Y = tmp_buffer->mouse_Y / 32768.0f;
	data.mouse_Z = tmp_buffer->mouse_Z / 32768.0f;

	// 判断鼠标触发
	judge_key(&data.mouse_left, tmp_buffer->mouse_left,
	          pre_uart_rx_data.mouse_left);
	judge_key(&data.mouse_right, tmp_buffer->mouse_right,
	          pre_uart_rx_data.mouse_right);

	// 判断键盘触发
	for (int i = 0; i < 16; i++) {
		judge_key(&data.key[i], ((tmp_buffer->key) >> i) & 0x1,
		          ((pre_uart_rx_data.key) >> i) & 0x1);
	}

	// 左前轮信息
	data.yaw = (tmp_buffer->channel_Yaw - rocker_offset) / rocker_num;

	// 保留数据
	memcpy(&pre_uart_rx_data, tmp_buffer, 18);
}

void DR16::UART_RxCallback(uint8_t *_rx_data)
{
	flag += 1;
	data_pross();
}

void DR16::TIM1msMod50_Alive_PeriodElapsedCallback()
{
	if (flag==pre_flag) {
		dr16_status=DR16_DISABLE;
	}
	else {
		dr16_status=DR16_ENABLE;
	}
	pre_flag=flag;
}
/*========================COPYRIGHT(C) CCUT-DAMOCLES==========================*/
